from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, rmdir
from conan.tools.scm import Version
import os

required_conan_version = ">=1.54.0"


class Tinyxml2Conan(ConanFile):
    name = "hwcpipe"
    description = "Hardware counters interface"
    license = "MIT"
    topics = ("GPU", "CPU", "counter")
    homepage = "https://github.com/ARM-software/HWCPipe"
    url = "https://github.com/conan-io/conan-center-index"

    package_type = "library"
    settings = "os", "arch", "compiler", "build_type"
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
    }
    default_options = {
        "shared": False,
        "fPIC": True,
    }

    def export_sources(self):
        copy(self, "CMakeLists.txt", self.recipe_folder, self.export_sources_folder)
        export_conandata_patches(self)

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")

    def source(self):
        os.rename("CMakeLists.txt","CMakeLists.txt.bak")
        get(self, **self.conan_data["sources"][self.version], strip_root=True)
        os.remove("CMakeLists.txt")
        os.rename("CMakeLists.txt.bak","CMakeLists.txt")

    def generate(self):
        tc = CMakeToolchain(self)
        tc.generate()

    def build(self):
        cmake = CMake(self)
        args = []
        args.append("-DHWCPIPE_WERROR=OFF")
        cmake.configure(cli_args=args)
        cmake.build()

    def package(self):
        copy(self, "LICENSE.md", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
        cmake = CMake(self)
        cmake.install()
        #copy(self, "libhwcpipe.a", src=self.source_folder + "/hwcpipe", dst=os.path.join(self.package_folder, "lib"))
        #copy(self, "include", src=self.source_folder + "/hwcpipe", dst=os.path.join(self.package_folder, "include"))

    def package_info(self):
        self.cpp_info.set_property("cmake_file_name", "hwcpipe")
        self.cpp_info.set_property("cmake_target_name", "hwcpipe")
        self.cpp_info.set_property("pkg_config_name", "hwcpipe")
        
        self.cpp_info.components["hwcpipe"].set_property("cmake_target_name", "hwcpipe::Hwcpipe")
        self.cpp_info.components["hwcpipe"].set_property("pkg_config_name", "hwcpipe")
        self.cpp_info.components["hwcpipe"].names["cmake_find_package"] = "hwcpipe"
        self.cpp_info.components["hwcpipe"].names["cmake_find_package_multi"] = "hwcpipe"
        self.cpp_info.components["hwcpipe"].libs = ["hwcpipe"]

        
        self.cpp_info.components["device"].set_property("cmake_target_name", "hwcpipe::Device")
        self.cpp_info.components["device"].set_property("pkg_config_name", "device")
        self.cpp_info.components["device"].names["cmake_find_package"] = "device"
        self.cpp_info.components["device"].names["cmake_find_package_multi"] = "device"
        self.cpp_info.components["device"].libs = ["device"]
